package com.test.fundible.fundible_06_loan_apply;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.fundible.OnOldUserChooseDayListener;
import com.test.fundible.R;
import com.test.fundible.fundible_00_http.OkHttpUtil;

import java.util.ArrayList;


public class LoanDayChooseViewAdapter extends RecyclerView.Adapter<LoanDayChooseViewAdapter.DayChooseHolder> {


    private int[] days = new int[0];

    private int chooseDay = 0;

    private final ArrayList<DayChooseHolder> holders = new ArrayList<>();

    private final OnOldUserChooseDayListener onOldUserChooseDayListener;

    public LoanDayChooseViewAdapter(OnOldUserChooseDayListener onOldUserChooseDayListener){
        this.onOldUserChooseDayListener = onOldUserChooseDayListener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setDays(int[] days) {
        holders.clear();
        if (days == null) days = new int[0];
        else this.days = days;
        notifyDataSetChanged();
    }

    public void setChooseDay(int chooseDay){
        this.chooseDay = chooseDay;
    }

    @NonNull
    @Override
    public DayChooseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DayChooseHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_day_choose,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull DayChooseHolder holder, int position) {

        if (!holders.contains(holder)) holders.add(holder);

        if (position<0 || position>=days.length) return;

        int day = days[position];

        holder.setDay(String.valueOf(day));

        if (chooseDay == day){
            holder.setChoose(true);
        }

        holder.getClickView().setOnClickListener(v -> {
            this.chooseDay = day;
            for (DayChooseHolder holder1:holders){
                holder1.setChoose(holder1.getDay().equalsIgnoreCase(String.valueOf(chooseDay)));
            }
            if (onOldUserChooseDayListener!=null) onOldUserChooseDayListener.onChooseDay(String.valueOf(chooseDay));
        });

    }

    @Override
    public int getItemCount() {
        return days.length;
    }

    public int getChooseDay() {
        return chooseDay;
    }

    public static class DayChooseHolder extends RecyclerView.ViewHolder{

        private final View bgView;
        private final ImageView chooseIv;
        private final TextView dayTv;

        private String day;

        @SuppressLint("InflateParams")
        public DayChooseHolder(View view){
            super(view);

            bgView = view.findViewById(R.id.idc_view_bg);
            chooseIv = view.findViewById(R.id.idc_iv_choose);
            dayTv = view.findViewById(R.id.idc_tv_day);

        }

        public void setDay(String day){
            if (!TextUtils.isEmpty(day)) {
                this.day = day;
                dayTv.setText(day);
            }else this.day = "";
        }

        public String getDay() {
            if (TextUtils.isEmpty(day)) return "";
            return day;
        }

        public void setChoose(boolean isChoose){
            if (isChoose){
                bgView.setBackgroundResource(R.drawable.bg_day_choose_light);
                chooseIv.setVisibility(View.VISIBLE);
            }else {
                bgView.setBackgroundResource(R.drawable.bg_day_choose_gray);
                chooseIv.setVisibility(View.GONE);
            }
        }

        public View getClickView(){
            return bgView;
        }

    }


}
